package com.douya.pangu.application.sys.service;

import com.douya.common.ex.EmptyResultException;
import com.douya.common.ex.ApiException;
import com.douya.common.enums.CommonResultEnums;
import com.douya.common.pojo.dto.PagerDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.douya.common.utils.BeanUtils;
import com.douya.pangu.business.api.pojo.dto.BusinessHttpLogDTO;
import com.douya.pangu.business.api.pojo.vo.BusinessHttpLogVO;
import com.douya.pangu.application.sys.mapper.BusinessHttpLogMapper;
import com.douya.pangu.application.sys.pojo.entity.BusinessHttpLogDO;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 说明：http请求日志服务类
 *
 * @author hcy
 * @date 2024/04/01
 */
@Slf4j
@Service
@Transactional(readOnly = true)
public class BusinessHttpLogService {

    @Resource
    BusinessHttpLogMapper mBusinessHttpLogMapper;

    /**
     * 分页获取http请求日志
     *
     * @param pager 分页条件
     * @return 分页信息
     */
    public IPage<BusinessHttpLogVO> page(PagerDTO pager) {
        BusinessHttpLogDO mBusinessHttpLog = new BusinessHttpLogDO();
        IPage<BusinessHttpLogDO> selectPage = mBusinessHttpLogMapper.selectPage(
        pager.toPage(mBusinessHttpLog),pager.toQuery(mBusinessHttpLog));
            return selectPage.convert(
            item -> BeanUtils.born(item, BusinessHttpLogVO.class));
    }

    /**
     * 根据ID获取http请求日志的VO对象
     *
     * @param id ID
     * @return http请求日志，匹配不到抛出异常
     */
    public BusinessHttpLogVO findById(Long id) {
        BusinessHttpLogDO businessHttpLog = mBusinessHttpLogMapper.selectById(id);
        if (businessHttpLog == null) {
            throw new EmptyResultException();
        }
        return BeanUtils.born(businessHttpLog, BusinessHttpLogVO.class);
    }

    /**
     * 新增/修改http请求日志
     *
     * @param businessHttpLogDTO http请求日志
     */
    @Transactional(rollbackFor = Throwable.class)
    public void insert(BusinessHttpLogDTO businessHttpLogDTO) {
        BusinessHttpLogDO businessHttpLogDO;
        if (businessHttpLogDTO.getId() != null) {
            businessHttpLogDO = mBusinessHttpLogMapper.selectById(businessHttpLogDTO.getId());
            if (businessHttpLogDO == null) {
                throw new ApiException("ID无法匹配", CommonResultEnums.INVALID_PARAMS.getCode());
            }
            BeanUtils.copyProperties(businessHttpLogDTO, businessHttpLogDO);
            mBusinessHttpLogMapper.updateById(businessHttpLogDO);
        } else {
            businessHttpLogDO = new BusinessHttpLogDO();
            BeanUtils.copyProperties(businessHttpLogDTO, businessHttpLogDO);
            mBusinessHttpLogMapper.insert(businessHttpLogDO);
        }
    }

    /**
     * 根据ID删除http请求日志
     *
     * @param id ID
     */
    @Transactional(rollbackFor = Throwable.class)
    public void delete(Long id) {
        mBusinessHttpLogMapper.deleteById(id);
    }

}
